/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var successCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            tip = '添加成功';
            window.setTimeout(function () {
                $('#requestCannel').click();
                window.location.reload();
            }, 1000);
            break;
    }
    tooltipWrong($('#requestAdd'), tip, 2);
}

var errorCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestAdd'), '系统内部错误', 3);
    } else {
        tooltipWrong($('#requestAdd'), data.responseJSON.info, 3);
    }
    $('#imgcode').trigger('click');
}
var successEditCallback = function (data) {
    var tip = '';
    switch (data.status) {
        case 200:
            tip = '修改成功';
            window.setTimeout(function () {
                $('#requestCannel').click();
                window.location.reload();
            }, 1000);
            break;
    }
    tooltipWrong($('#requestEdit'), tip, 2);
}

var errorEditCallback = function (data) {
    if (typeof (data.responseJSON) == "undefined") {
        tooltipWrong($('#requestEdit'), '系统内部错误', 3);
    } else {
        tooltipWrong($('#requestEdit'), data.responseJSON.info, 3);
    }
    $('#imgcode').trigger('click');
}

$(function () {
    var vcodeResult,zhnameResult,twnameResult, ennameResult,
        zhdescResult,twdescResult,endescResult,signResult;
    $("#requestZhName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            zhnameResult = false;
            window.tip = "简体服务名称不能为空";
        }
        zhnameResult = true;
    });
    $("#requestTwName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            twnameResult = false;
            window.tip = "繁体服务名称不能为空";
        }
        twnameResult = true;
    });
    $("#requestEnName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            ennameResult = false;
            window.tip = "英文服务名称不能为空";
        }
        ennameResult = true;
    });
    $('#requestZhDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            zhdescResult = false;
            window.tip = "简体描述不能为空";
        }
        zhdescResult = true;
    });
    $('#requestTwDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            twdescResult = false;
            window.tip = "繁体描述不能为空";
        }
        twdescResult = true;
    });
    $('#requestEnDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            endescResult = false;
            window.tip = "英文描述不能为空";
        }
        endescResult = true;
    });
    $('#requestServiceSign').on('blur', function (e) {
        e.preventDefault();
        var sign = $(this).val().trim();
        if (sign == '') {
            signResult = false;
            window.tip = "服务标识不能为空";
        }
        signResult = true;
    });
    $('#requestVcode').on('blur', function (e) {
        e.preventDefault();
        switch (validateData($(this).val(), /^\w{4}$/)) {
            case 0:
                vcodeResult = false;
                window.tip = "验证码为4位字符";
                break;
            case -1:
                vcodeResult = false;
                window.tip = "验证码不能为空";
                break;
            case 1:
                vcodeResult = true;
                break;
            default:
        }
    });

    $('#requestAdd').on('click', function (e) {
        e.preventDefault();
        window.tip = false;
        $('#requestVcode').blur();
        $('#requestServiceSign').blur();
        $('#requestEnDescription').blur();
        $('#requestEnName').blur();
        $('#requestTwDescription').blur();
        $('#requestTwName').blur();
        $('#requestZhDescription').blur();
        $('#requestZhName').blur();
        if(window.tip){
            tooltipWrong($('#requestAdd'), window.tip, 3);
        }
        if (vcodeResult && zhnameResult && twnameResult && ennameResult && zhdescResult && twdescResult && endescResult && signResult) {
            var params = new Object();
            params.zhname = $('#requestZhName').val();
            params.twname = $('#requestTwName').val();
            params.enname = $('#requestEnName').val();
            params.zhdescription = $('#requestZhDescription').val().trim();
            params.twdescription = $('#requestTwDescription').val().trim();
            params.endescription = $('#requestEnDescription').val().trim();
            params.vcode = $('#requestVcode').val();
            params.sign = $('#requestServiceSign').val();
            ajaxRequest('/Home/InterfaceServer/addInterfaceServer', params, 'post', successCallback, errorCallback);
        }
    });

    //编辑
    var vcodeEditResult,zhnameEditResult,twnameEditResult, ennameEditResult,
        zhdescEditResult,twdescEditResult,endescEditResult,signEditResult;
    $('.edit').on('click', function (e) {
        e.preventDefault();
        //清除原数据
        $('#requestId').val(0);
        $('#requestEditName').val('');
        $('#requestEditDescription').val('');
        var obj = JSON.parse($(this).attr("data-json"));
        $('#requestId').val(obj.id);
        $('#requestEditCnName').val(obj.language[0].name);
        $('#requestEditCnDescription').val(obj.language[0].description);
        $('#requestEditTwName').val(obj.language[1].name);
        $('#requestEditTwDescription').val(obj.language[1].description);
        $('#requestEditEnName').val(obj.language[2].name);
        $('#requestEditEnDescription').val(obj.language[2].description);
    });

    $("#requestEditCnName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            zhnameEditResult = false;
            window.tipEdit = "简体服务名称不能为空";
        }
        zhnameEditResult = true;
    });
    $("#requestEditTwName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            twnameEditResult = false;
            window.tipEdit = "繁体服务名称不能为空";
        }
        twnameEditResult = true;
    });
    $("#requestEditEnName").on('blur', function (e) {
        e.preventDefault();
        var name = $(this).val();
        if (name == '') {
            ennameEditResult = false;
            window.tipEdit = "英文服务名称不能为空";
        }
        ennameEditResult = true;
    });
    $('#requestEditCnDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            zhdescEditResult = false;
            window.tipEdit = "简体描述不能为空";
        }
        zhdescEditResult = true;
    });
    $('#requestEditTwDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            twdescEditResult = false;
            window.tipEdit = "繁体描述不能为空";
        }
        twdescEditResult = true;
    });
    $('#requestEditEnDescription').on('blur', function (e) {
        e.preventDefault();
        var description = $(this).val().trim();
        if (description == '') {
            endescEditResult = false;
            window.tipEdit = "英文描述不能为空";
        }
        endescEditResult = true;
    });

    $('#requestEditVcode').on('blur', function (e) {
        e.preventDefault();
        switch (validateData($(this).val(), /^\w{4}$/)) {
            case 0:
                vcodeEditResult = false;
                window.tipEdit = "验证码为4位字符";
                break;
            case -1:
                vcodeEditResult = false;
                window.tipEdit = "验证码不能为空";
                break;
            case 1:
                vcodeEditResult = true;
                break;
            default:
        };
    });
    $('#requestEditServiceSign').on('blur', function (e) {
        e.preventDefault();
        var sign = $(this).val().trim();
        if (sign == '') {
            signEditResult = false;
            window.tipEdit = "服务标识不能为空";
        }
        signEditResult = true;
    });
    $('#requestEdit').on('click', function (e) {
        e.preventDefault();
        window.tipEdit = false;
        $('#requestEditVcode').blur();
        $('#requestEditServiceSign').blur();
        $('#requestEditEnDescription').blur();
        $('#requestEditEnName').blur();
        $('#requestEditTwDescription').blur();
        $('#requestEditTwName').blur();
        $('#requestEditCnDescription').blur();
        $('#requestEditCnName').blur();
        if(window.tipEdit){
            tooltipWrong($('#requestEdit'),window.tipEdit, 3);
        }       
        if (vcodeEditResult && zhnameEditResult && twnameEditResult && ennameEditResult && zhdescEditResult && twdescEditResult && endescEditResult && signEditResult) {
            var params = new Object();
            params.zhname = $('#requestEditCnName').val();
            params.twname = $('#requestEditTwName').val();
            params.enname = $('#requestEditEnName').val();
            params.serverId = $('#requestId').val();
            params.zhdescription = $('#requestEditCnDescription').val().trim();
            params.twdescription = $('#requestEditTwDescription').val().trim();
            params.endescription = $('#requestEditEnDescription').val().trim();
            params.vcode = $('#requestEditVcode').val();
            params.sign = $('#requestEditServiceSign').val();
            ajaxRequest('/Home/InterfaceServer/saveInterfaceServer', params, 'post', successEditCallback, errorEditCallback);
        }
    });

});